import requests
import base64
import os, json

access_token = os.environ['baiduocr_hw']
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}

for i in os.listdir():
    f = open(i, 'rb')
    img = base64.b64encode(f.read())
    params = {"image":img}
    
    response = requests.post(request_url, data=params, headers=headers)
        
    if response:
        v1 = response.json()['words_result']
        v3 = ''
        for i2 in v1:
            v3 += i2['words']
            
        print(v3)

        with open(i+'.txt', 'w', encoding='utf8') as f1:
            f1.write(v3)
            
